package me.zhanshi123.lab4.controller;

import me.zhanshi123.lab4.entity.Good;
import me.zhanshi123.lab4.entity.GoodFilter;
import me.zhanshi123.lab4.service.IGoodService;
import me.zhanshi123.lab4.service.impl.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/api/goods")
public class GoodController {
    @Autowired
    private IGoodService goodService;

    private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @RequestMapping("/add")
    public String add(
            @RequestParam(name = "name") String name,
            @RequestParam(name = "type") String type,
            @RequestParam(name = "origin") String origin,
            @RequestParam(name = "price") double price,
            @RequestParam(name = "production") String production,
            @RequestParam(name = "manufacturer") String manufacturer
    ) {
        Date date = null;
        try {
            date = simpleDateFormat.parse(production);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return goodService.addGood(new Good(-1, name, type, origin, price, date, manufacturer));
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam("id") int id) {
        return goodService.deleteGood(id);
    }

    @RequestMapping("/statistics")
    public String statistics() {
        return goodService.getStatistics();
    }

    @RequestMapping("/update")
    public String update(
            @RequestParam(name = "id") int id,
            @RequestParam(name = "name") String name,
            @RequestParam(name = "type") String type,
            @RequestParam(name = "origin") String origin,
            @RequestParam(name = "price") double price,
            @RequestParam(name = "production") String production,
            @RequestParam(name = "manufacturer") String manufacturer) {
        Date date = null;
        try {
            date = simpleDateFormat.parse(production);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return goodService.editGood(new Good(id, name, type, origin, price, date, manufacturer));
    }

    @RequestMapping("/show")
    public String show(@RequestParam(name = "span") int span,
                       @RequestParam(name = "start") int start,
                       @RequestParam(name = "filter[id]") int id,
                       @RequestParam(name = "filter[name]") String name,
                       @RequestParam(name = "filter[type]") String type,
                       @RequestParam(name = "filter[origin]") String origin,
                       @RequestParam(name = "filter[manufacturer]") String manufacturer
    ) {

        GoodFilter goodFilter = new GoodFilter(new Good(id, name, type, origin, 0.0, null, manufacturer));
        goodFilter.setStart(start);
        goodFilter.setSpan(span);
        return goodService.showGoods(goodFilter);
    }
}
